#include <stdio.h>
#include <pthread.h>
int st[10] = {1,2,3,4,5,6,7,8,9,0};
pthread_mutex_t lock;
pthread_cond_t cond;
int i;
void *handler_printf(void *arg) {
    while(1) {
        pthread_mutex_lock(&lock);
        pthread_cond_wait(&cond, &lock);
        for(int i=0; i<10; i++) {
            printf("%d ", st[i]);
        }
        printf("\n");
        pthread_mutex_unlock(&lock);
    }
    pthread_exit(NULL);
}

void *handler_swap(void *arg) {
    while(1){
        pthread_mutex_lock(&lock);
        for(i = 0; i< 5; i++) {
            int t = st[i];
            st[i] = st[9-i];
            st[9-i] = t;
        }
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&lock);
        sleep(1);
    }
    pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
    pthread_t tid1, tid2;
    if(pthread_cond_init(&cond, NULL)<0) {
        perror("cond init err\n");
        return -1;
    }
    if(pthread_mutex_init(&lock, NULL)<0) {
        perror("mutex init err\n");
        return -1;
    }
    if(pthread_create(&tid1, NULL, handler_swap, NULL) != 0) {
        perror("pthread create err\n");
        return -1;
    }
    if(pthread_create(&tid2, NULL, handler_printf, NULL) != 0) {
        perror("pthread create err\n");
        return -1;
    }
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    return 0;
}